//
//  UserMessage.swift
//  LoveFreshBeen
//
//  Created by Gao on 17/2/13.
//  Copyright © 2017年 Odm. All rights reserved.
//

import UIKit

enum UserMessageType: Int {
    case system = 0
    case user = 1
}

class UserMessage: NSObject {
    
    var id: String?
    var type = -1
    var title: String?
    var content: String?
    var link: String?
    var city: String?
    var noticy: String?
    var send_time: String?
    
    // 辅助参数
    var subTitleViewHeightNomarl: CGFloat = 60
    var cellHeight: CGFloat = 60 + 60 + 20
    var subTitleViewHeightSpread: CGFloat = 0
    
    class func loadSystemMessage(_ complete: ((_ data: [UserMessage]?, _ error: NSError?) -> ())) {
        
        complete(loadMessage(.system)!, nil)
    }
    
    class func loadUserMessage(_ complete: ((_ data: [UserMessage]?, _ error: NSError?) -> ())) {
        complete(loadMessage(.user), nil)
    }
    
    fileprivate class func loadMessage(_ type: UserMessageType) -> [UserMessage]? {
        
        var data: [UserMessage]? = []
        
        
        guard let path = Bundle.main.path(forResource: ((type == .system) ? "SystemMessage" : "UserMessage"), ofType: nil),
            let resData = try? Data(contentsOf: URL(fileURLWithPath: path)),
            let dict = (try? JSONSerialization.jsonObject(with: resData, options: .allowFragments)) as? NSDictionary,
            let array = dict.object(forKey: "data") as? NSArray
            else{
                return data
        }
        
        for dict in array {
            guard let message = UserMessage.yy_model(withJSON: dict) else { continue }
            data?.append(message)
        }
        
        return data
    }
    
    
}
